﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using mojoPortal.Web.RS.AppCode.Lib;

namespace mojoPortal.Web.RS
{
    public class RSBasePage : mojoBasePage
    {
        //public RSUserIdentity _userIdentity;

        protected Label lblRSCompanyName;
        protected Label lblRSUser;
        public RSBasePage()
        {
            
            //SuppressAllMenus();
            //SuppressGoogleAds();
            //SuppressMenuSelection();
            //SuppressPageMenu();
            
        }

        public RSUserIdentity GetRSUserIdentity()
        {
            RSUserIdentity userIdentity = null;
            if (Session["USER_IDENTITY"] != null)
            {
                userIdentity = (RSUserIdentity)Session["USER_IDENTITY"];
            }
            else
            {
                userIdentity = new RSUserIdentity();
                Session["USER_IDENTITY"] = userIdentity;
            }

            return userIdentity;
        }

        public string GetRSCompanyConnectionString()
        {
            string rsConnectionString = string.Empty;
            if (Session["COMPANY_CONNECTIONSTRING"] != null)
            {
                rsConnectionString = Session["COMPANY_CONNECTIONSTRING"].ToString();
            }
            else
            {
                rsConnectionString = RSCommonFunctions.GetRSCompanyConnectionString();
                Session["COMPANY_CONNECTIONSTRING"] = rsConnectionString;
            }
            return rsConnectionString;
        }

        public bool ValidateReadyServiceUserPIN()
        {
            bool val = false;
            if (Session["ISVALID_USERPIN"] != null)
            {
                val = (bool)Session["ISVALID_USERPIN"];
            }
            else
            {
                if (RSCommonFunctions.ValidateReadyServiceUserPIN())
                    val = true;
                else
                    val = false;
            }

            Session["ISVALID_USERPIN"] = val;
            return val;
        }

        public void SetPrevURL()
        {
            if (Request.UrlReferrer != null)
            {
                string refererurl = Request.UrlReferrer.ToString();
                string prevurl = string.Empty;
                if (Session["PreviousURL"] != null)
                    prevurl = Session["PreviousURL"].ToString();

                if (prevurl != refererurl)
                    Session["PreviousURL"] = Request.UrlReferrer.ToString();
            }

        }
        public void ValidateUserSession()
        {
            if (Context.Session == null)
            {
                HttpContext.Current.Response.Clear();
                Session.Abandon();
                Response.Redirect("~/Secure/Login.aspx");
            }

            if (ValidateReadyServiceUserPIN() == false)
                Response.Redirect("~/RS/Secure/RSAccessDenied.aspx?code=unknownpin");

            RSUserIdentity userIdentity = GetRSUserIdentity();
            if (!userIdentity.RSIsUserAuthenticated)
                Response.Redirect("~/RS/Secure/RSAccessDenied.aspx?code=denied");

        }
        public void InitializeReadyServicePage(string pgheader = "")
        {
            if (!Page.IsPostBack)
                SetPrevURL();
            ValidateUserSession();

            SuppressPageMenu();
            Panel pnl = (Panel)Master.FindControl("divReadyServicePageHeader");
            if (pnl != null)
                pnl.Visible = true;

            Panel pnlRSBanner = (Panel)Master.FindControl("pnlRSBanner");
            if (pnlRSBanner != null)
                pnlRSBanner.Visible = false;

            //lblRSCompanyName = (Label)Master.FindControl("lblReadyServiceCompanyName");
            //lblRSCompanyName.Text = userIdentity.RSCompanyName;
            //lblRSUser = (Label)Master.FindControl("lblReadyServiceUser");
            //lblRSUser.Text = userIdentity.RSFirstName + " " + userIdentity.RSLastName;

            //mojoPortal.Web.UI.SiteTitle lblSiteTitle = (mojoPortal.Web.UI.SiteTitle)Master.FindControl("SiteTitle");
            //lblSiteTitle.OverrideTitle = userIdentity.RSCompanyName;

            if(pgheader != "")
                SetRSPageHeader(pgheader);
        }

        public void SetRSPageHeader(string pgheader)
        {
            Label lblRSPageHeader = (Label)Master.FindControl("lblReadyServicePageHeader");
            if(lblRSPageHeader != null)
                lblRSPageHeader.Text = pgheader;
        }
    }

}